package com.qgqaqgqa.deviceinfomanager.db;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.qgqaqgqa.deviceinfomanager.model.DeviceInfoModel;
import com.qgqaqgqa.deviceinfomanager.util.utilcode.util.FileIOUtils;
import com.qgqaqgqa.deviceinfomanager.util.utilcode.util.LogUtils;
import com.qgqaqgqa.deviceinfomanager.util.utilcode.util.StringUtils;
import com.qgqaqgqa.deviceinfomanager.util.utilcode.util.TimeUtils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 最新数据处理
 * Created by qian on 2017/2/8 0008.
 */
public class DeviceDao {
    private static Object OSSLock = new Object();
    private DBOpenHelper helper;

    public DeviceDao(Context paramContext) {
        this.helper = new DBOpenHelper(paramContext);
    }

    /**
     * 删除所有
     *
     * @return
     */
    public int deleteAll() {
        SQLiteDatabase db = this.helper.getWritableDatabase();
        synchronized (OSSLock) {
            int i = db.delete(DBOpenHelper.TABLE_DEVICE, null, null);
            db.close();
            return i;
        }
    }
    /**
     * 删除所有
     *
     * @return
     */
    public int deleteOld(String time) {
        SQLiteDatabase db = this.helper.getWritableDatabase();
        synchronized (OSSLock) {
            int i = db.delete(DBOpenHelper.TABLE_DEVICE, "endTime < ?", new String[]{time});
            db.close();
            return i;
        }
    }

    public long insert(DeviceInfoModel m) {
        delete(m.getId());
        SQLiteDatabase db = this.helper.getWritableDatabase();
        synchronized (OSSLock) {
            long l = db.insert(DBOpenHelper.TABLE_DEVICE, null, m.getContentValues());
            db.close();
            return l;
        }
    }

    public void insertList(List<Map<String, String>> list, FileIOUtils.OnProgressUpdateListener listener) {
        deleteAll();
        SQLiteDatabase db = this.helper.getWritableDatabase();
        synchronized (OSSLock) {
            ContentValues values = new ContentValues();
            double allCount = list.size();
            double curCount = 0;
            for(Map<String, String> m:list){
                if(m.containsKey("ID")&&m.get("ID") != null &&
                        !"null".equals(m.get("ID")) && !"".equals(m.get("ID"))){
                    values.put("id", (m.get("ID") + "").trim().toUpperCase());
                }else{
                    continue;
                }
                StringBuffer sb=new StringBuffer();
                StringBuffer sbAll=new StringBuffer();
                for(String key:m.keySet()){
                    if(!"ID".equals(key)){
                        if(sb.length()>0){
                            sb.append("  ");
                            sbAll.append("  ");
                        }
                        sb.append(m.get(key));
                        sbAll.append(key+"  "+m.get(key));
                    }
                }
                values.put("name", sb.toString());
                values.put("allname", sbAll.toString());
                db.insertWithOnConflict(DBOpenHelper.TABLE_DEVICE, "id", values, SQLiteDatabase.CONFLICT_IGNORE);

                listener.onProgressUpdate(++curCount / allCount);
            }
            db.close();
        }

    }
    /**
     * 查询全部项目
     *
     * @return
     */
    public ArrayList<DeviceInfoModel> query() {
        SQLiteDatabase db = this.helper.getReadableDatabase();
        synchronized (OSSLock) {
            ArrayList<DeviceInfoModel> areaList = new ArrayList<>();
            Cursor cursor = db.query(DBOpenHelper.TABLE_DEVICE, null,
                    null, null, null, null, null);
            if (cursor != null) {
                while (cursor.moveToNext()) {
                    areaList.add(new DeviceInfoModel(cursor));
                }
            }
            cursor.close();
            db.close();
            return areaList;
        }
    }
    /**
     * 查询项目
     *
     * @return
     */
    public ArrayList<DeviceInfoModel> query(String text,int page) {
        SQLiteDatabase db = this.helper.getReadableDatabase();
        synchronized (OSSLock) {
            ArrayList<DeviceInfoModel> list = new ArrayList<>();
            Cursor cursor;
            String select=null;
            String[] selectValue=null;
            if(!StringUtils.isBlank(text)){
                select="id like ? or name like ?";
                selectValue = new String[]{"%"+text+"%", "%"+text+"%"};
            }
            cursor = db.query(DBOpenHelper.TABLE_DEVICE, null,
                    select, selectValue, null, null, "name ASC limit 20 offset "+page*20);
            if (cursor != null) {
                while (cursor.moveToNext()) {
                    list.add(new DeviceInfoModel(cursor));
                }
            }
            cursor.close();
            db.close();
            return list;
        }
    }

    /**
     * 删除
     *
     * @return
     */
    public int delete(String id) {
        SQLiteDatabase db = this.helper.getWritableDatabase();
        synchronized (OSSLock) {
            int i = db.delete(DBOpenHelper.TABLE_DEVICE, "id=?",
                    new String[]{id});
            db.close();
            return i;
        }
    }

    public long getQueryCount(String text){
        String select="";
        String[] selectValue=null;
        SQLiteDatabase db = this.helper.getReadableDatabase();
        synchronized (OSSLock) {
            if(!StringUtils.isBlank(text)) {
                select = " where id like ? or name like ? ";
                selectValue = new String[]{"%" + text + "%", "%" + text + "%"};
            }
            Cursor cursor = db.rawQuery("select count(*) from "+DBOpenHelper.TABLE_DEVICE+select,selectValue);
            cursor.moveToFirst();

            Long count = cursor.getLong(0);
            cursor.close();
            db.close();
            return count;
        }
    }
}
